<?php
// +----------------------------------------------------------------------
// | Citrix [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.tpCitrix.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.tpCitrix.com/ )
// +----------------------------------------------------------------------
// | Author: Citrix < 2722279500@qq.com >
// +----------------------------------------------------------------------

namespace app\index\controller;

use \think\Db;
use \think\Cache;
use \think\Cookie;
use \think\Session;
use app\index\controller\Base;
class Goods extends Base
{
	public function __construct()
    {
        parent::__construct();
        $cate_model = Db::name('goods_cate');
       	$goods_cate = $cate_model->where('pid',0)->order('sort DESC')->select();
       	if(!empty($goods_cate))
       	{
       		foreach ($goods_cate as $k => $v) 
       		{
       			$goods_cate[$k]['cate2'] = $cate_model->where('pid',$goods_cate[$k]['id'])->order('sort DESC')->select();
       			if(!empty($goods_cate[$k]['cate2']))
       			{
       				foreach ($goods_cate[$k]['cate2'] as $key => $value) 
       				{
       					$goods_cate[$k]['cate2'][$key]['cate3'] = $cate_model->where('pid',$goods_cate[$k]['cate2'][$key]['id'])->order('sort DESC')->select();
       				}
       			}
       		}
       	}
       	$this->goods_cate = $goods_cate;
        $this->assign('cur',"goods");
        $this->assign('goods_cate',$goods_cate);
    }
    public function index()
    {
    	$cate_id = input('cate_id/d',$this->goods_cate[0]['id']);
    	$page = input('page/d',1);
    	$count = Db::name('goods')->where(['status'=>1,'goods_cate_id'=>$cate_id])->count();
    	$limit = $page-1;
    	$goods = Db::name('goods')->where(['status'=>1,'goods_cate_id'=>$cate_id])->order('sort DESC')->limit($limit,12)->select();
    	$pager = pager($count,$page,12,6,'/goods/index/cate_id/'.$cate_id);
    	$this->assign('pager',htmlspecialchars_decode($pager));
      $this->assign('goods',$goods);
      $this->assign('cate_id',$cate_id);
      $this->assign('web_title',get_goods_cate_name($cate_id).'-下载中心');
        return $this->fetch();
    }
    public function info()
    {
    	$id = input('id/d',0);
    	$cate_id = input('cate_id/d',$this->goods_cate[0]['id']);
    	$info = Db::name('goods')->where('id',$id)->find();
    	if(!empty($info))
    	{
    		$click = $info['click']+1;
    		Db::name('goods')->where('id',$id)->update(['click'=>$click]);
    	}
        $last = Db::name('goods')->where(['goods_cate_id'=>$cate_id])->where('id','lt',$id)->find();
        $next = Db::name('goods')->where(['goods_cate_id'=>$cate_id])->where('id','gt',$id)->find();
        $this->assign('last',$last);
        $this->assign('next',$next);
        $this->assign('info',$info);
        $this->assign('cate_id',$cate_id);
        $this->assign('web_title',$info['title'].'-'.get_goods_cate_name($cate_id).'-下载中心');
        return $this->fetch();
    }
}